/**
 * Copyright (C) 2010 - 2013 Harry Glasgow
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.jaden.ui;

import com.googlecode.jaden.common.other.ReopenMap;
import com.googlecode.jaden.common.other.Utils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class MenuBar extends JMenuBar {

    private UI userInterface;
    private JMenu reopenMenu;
    private Actions actions;
    private ActionListener reopenActionListener;

    public MenuBar(UI userInterface, Actions actions) {
        this.actions = actions;
        this.userInterface = userInterface;
        reopenActionListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                reopen(e);
            }
        };
        JMenu fileMenu = addMenu(Constants.MENU_FILE_NAME, Constants.MNEMONIC_FILE);
        JMenu newMenu = addMenu(fileMenu, Constants.MENU_NEW_NAME, Constants.MNEMONIC_NEW);
        addMenuItem(newMenu, actions.getNewManual(), Constants.MNEMONIC_NEW_MANUAL);
        addMenuItem(newMenu, actions.getNewAutomatic(), Constants.MNEMONIC_NEW_AUTOMATIC);
        addMenuItem(fileMenu, actions.getOpenProjectAction(), Constants.MNEMONIC_OPEN_PROJECT);
        reopenMenu = addMenu(fileMenu, Constants.MENU_REOPEN_NAME, Constants.MNEMONIC_REOPEN_PROJECT);
        reopenMenu.setEnabled(false);
        addMenuItem(fileMenu, actions.getCloseProjectAction(), Constants.MNEMONIC_CLOSE_PROJECT);
        addSeparator(fileMenu);
        addMenuItem(fileMenu, actions.getSaveProjectAction(), Constants.MNEMONIC_SAVE_PROJECT);
        addMenuItem(fileMenu, actions.getSaveProjectAsAction(), Constants.MNEMONIC_SAVE_PROJECT_AS);
        addSeparator(fileMenu);
        addMenuItem(fileMenu, actions.getPreferencesAction(), Constants.MNEMONIC_PREFERENCES);
        addSeparator(fileMenu);
        addMenuItem(fileMenu, actions.getExitAction(), Constants.MNEMONIC_EXIT);

        JMenu configureMenu = addMenu(Constants.MENU_CONFIGURE_NAME, Constants.MNEMONIC_CONFIGURE);
        addMenuItem(configureMenu, actions.getEditProjectDetailsAction(), Constants.MNEMONIC_PROJECT_DETAILS);
        addMenuItem(configureMenu, actions.getEditProjectDefinitionAction(), Constants.MNEMONIC_PROJECT_DEFINITION);
        addMenuItem(configureMenu, actions.getEditTrainingFilesAction(), Constants.MNEMONIC_TRAINING_FILES);
        addMenuItem(configureMenu, actions.getEditFileMappingsAction(), Constants.MNEMONIC_FILE_MAPPINGS);

        JMenu trainingMenu = addMenu(Constants.MENU_TRAINING_NAME, Constants.MNEMONIC_TRAINING);
        addMenuItem(trainingMenu, actions.getManualTrainingAction(), Constants.MNEMONIC_MANUAL_TRAINING);
        addMenuItem(trainingMenu, actions.getAutomatedTrainingAction(), Constants.MNEMONIC_AUTOMATED_TRAINING);

        JMenu helpMenu = addMenu(Constants.MENU_HELP_NAME, Constants.MNEMONIC_HELP);
        addMenuItem(helpMenu, actions.getAboutAction(), Constants.MNEMONIC_ABOUT);
    }

    private static void addSeparator(JMenu fileMenu) {
        fileMenu.addSeparator();
    }

    private static void addMenuItem(JMenu menu, Action action, String mnemonic) {
        JMenuItem menuItem = new JMenuItem(action);
        menuItem.setMnemonic(Utils.translate(mnemonic).charAt(0));
        menu.add(menuItem);
    }

    private JMenu addMenu(String name, String mnemonic) {
        JMenu menu = new JMenu(Utils.translate(name));
        menu.setMnemonic(Utils.translate(mnemonic).charAt(0));
        add(menu);
        return menu;
    }

    private static JMenu addMenu(JMenu parent, String name, String mnemonic) {
        JMenu menu = new JMenu(Utils.translate(name));
        menu.setMnemonic(Utils.translate(mnemonic).charAt(0));
        parent.add(menu);
        return menu;
    }

    @SuppressWarnings({"NonConstantStringShouldBeStringBuffer"})
    public void updateReopenList(ReopenMap reopenMap) {
        reopenMenu.removeAll();
        if (reopenMap.getSize() == 0) {
            reopenMenu.setEnabled(false);
        } else {
            reopenMenu.setEnabled(true);
            for (int i = 0; i < reopenMap.getSize(); i++) {
                File file = reopenMap.getFile(i);
                boolean modified = false;
                String s = file.getAbsolutePath();
                while (s.length() > 30 && s.contains(File.separator)) {
                    int pos = s.indexOf(File.separator);
                    s = s.substring(pos + 1, s.length());
                    modified = true;
                }
                if (modified) {
                    s = "..." + File.separator + s;
                }
                JMenuItem m = new JMenuItem((1 + i) + " " + s);
                m.addActionListener(reopenActionListener);
                reopenMenu.add(m);
            }
            reopenMenu.addSeparator();
            reopenMenu.add(actions.getClearReopenAction());
        }
    }

    private void reopen(ActionEvent e) {
        String[] strings = e.getActionCommand().split("\\s");
        int key = Integer.parseInt(strings[0]) - 1;
        userInterface.doReopen(key);
    }
}
